package customerEnter.Frame.JDBCDao.daoFrame;

import java.sql.SQLException;
import java.util.ArrayList;

import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.WebAppsInfo;
import customerEnter.Frame.Enum.T_SQLConditionEnum;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameIntermediateLayerInterface;
import customerEnter.Frame.JDBCDao.Interface.InitializationInterface;
import customerEnter.tool.WhetherToEmpty;
import customerEnter.tool.springBeans;

/**
 * 初始化數據類 單例模式 框架的二層助手類
 * 
 * 类名称：initializationData 类描述： 创建人：dy 创建时间：2016-4-26 上午9:22:47
 * 
 */
public class InitializationData implements InitializationInterface {

	/**
	 * 初始化框架對象
	 */
	public <T, K> void initializationDataFunction(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		initializationEhcache(daoFramePameTransmission);
		initializationDaoFramelistOther(daoFramePameTransmission);
	}

	/**
	 * 初始化數據傳輸類
	 */
	public <T, K> DaoFramePameTransmission<T, K> initializationDaoFramePameTransmission(
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		if (WhetherToEmpty.ObjectWhetherToEmpty(daoFramePameTransmission)) {
			daoFramePameTransmission = new DaoFramePameTransmission<T, K>();
		}
		return daoFramePameTransmission;
	}

	/**
	 * 初始化中間方法類
	 * 
	 * @return
	 */
	public <T, K> DaoFrameIntermediateLayerInterface<T, K> initializationDaoFrameTransmission(
			DaoFrameIntermediateLayerInterface<T, K> daoTransmission) {
		if (WhetherToEmpty.ObjectWhetherToEmpty(daoTransmission)) {
			daoTransmission = new DaoFrameMiddleware<T, K>();
		}
		try {
			if (WhetherToEmpty.StringWhetherToEmpty(WebAppsInfo.webDataBase)) {
				WebAppsInfo.webDataBase = springBeans.getJdbcTemplate().getDataSource().getConnection().getMetaData()
						.getDriverName();
			}
		} catch (SQLException e) {
			System.out.println("數據庫信息讀取失敗:" + e.getMessage());
		}
		return daoTransmission;
	}

	/**
	 * 初始化緩存
	 */
	private <T, K> void initializationEhcache(DaoFramePameTransmission<T, K> daoFramePameTransmission) {

		if (daoFramePameTransmission.ecacheDataTransmission == null) {
			// daoFramePameTransmission.ecacheDataTransmission =
			// EcacheDataTransmission.getEcacheDataTransmission();
		}
	}

	/**
	 * 初始化list
	 */
	@SuppressWarnings("rawtypes")
	private <T, K> void initializationDaoFramelistOther(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		if (WhetherToEmpty.ListWhetherToEmpty(daoFramePameTransmission.DaoFramelistOther)) {
			daoFramePameTransmission.DaoFramelistOther = new ArrayList();
		} else {
			daoFramePameTransmission.DaoFramelistOther.clear();
		}
	}

	/**
	 * 初始化sql控制 控制查找 數據庫
	 */
	public void initializationSqlController() {
		SqlDataBaseController.setDataBase(T_SQLConditionEnum.DataBase);
		SqlDataBaseController.setDMLSele(T_SQLConditionEnum.DMLSelect);
		SqlDataBaseController.setFront(T_SQLConditionEnum.Front);
		SqlDataBaseController.setRule(T_SQLConditionEnum.Rule);
		SqlDataBaseController.setDMLalter(T_SQLConditionEnum.DMLinsert);
		SqlDataBaseController.setDMLdelete(T_SQLConditionEnum.DMLdelete);
		SqlDataBaseController.setDMLdrop(T_SQLConditionEnum.DMLdrop);
		SqlDataBaseController.setDMLinsert(T_SQLConditionEnum.DMLinsert);
		SqlDataBaseController.setDMLSet(T_SQLConditionEnum.DMLSet);
		SqlDataBaseController.setWhere(T_SQLConditionEnum.Where);
	}

}
